package br.edu.ufcg.sri;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;


public class EscutadorDeChave implements KeyListener {
	
	private List<String> words;
	
	private List<String> showWords;

	private JTable table;

	
	public EscutadorDeChave(List<String> wordis, JTable table){
		this.words = wordis;
		this.table = table;
		showWords = new ArrayList<String>();
	}

	public void keyPressed(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void keyTyped(KeyEvent arg0) {
		char c = arg0.getKeyChar();
		JTextField textField = (JTextField) arg0.getSource();
		String typedWord  = textField.getText();
		if(c != 8){
			typedWord += c;
		}
		showWords = new ArrayList<String>();
		for(String word : words){
			if(word.startsWith(typedWord)){
				showWords.add(word);
			}
		}
		Object[] array = showWords.toArray();
		Object[][] data = new Object[array.length][1];
		int i = 0;
		for(Object o: array){
			data[i][0] = o;
			i++;
		}

		String[] colum = { "" };
		
		DefaultTableModel nModel = new DefaultTableModel();
		nModel.setDataVector(data, colum);
		table.setModel(nModel);
		nModel.fireTableStructureChanged();
		
	}
	
	public List<String> getShowWords(){
		return showWords;
	}

}
